﻿using System;
using Entitas;
using UnityEngine;

public sealed class PlayerControllerSystem : IExecuteSystem
{
    public void Execute()
    {
        var entities = Contexts.sharedInstance.game
            .GetGroup(GameMatcher.AllOf(GameMatcher.Input, GameMatcher.Velocity))
            .GetEntities();

        foreach (var e in entities)
        {
            InputOp inputOp = e.input.inputOp;
            Vector3 dir = Vector3.zero;
            if (inputOp.left)
            {
                dir += Vector3.left;
            }

            if (inputOp.right)
            {
                dir += Vector3.right;
            }

            if (inputOp.forward)
            {
                dir += Vector3.forward;
            }

            if (inputOp.back)
            {
                dir += Vector3.back;
            }

            dir.Normalize();
            e.velocity.value = e.velocity.max * dir;
            e.velocity.Truncate();
        }
    }
}